package org.theSeed.base.utils;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import org.theSeed.base.pojo.constant.SysConstant;

import java.util.List;

/**
 * 字符串工具
 */
public class SeedStringUtil {
    /**
     * 字符串凭借工具
     * @param texts
     * @return
     */
    public static String concatStr(Object... texts){
        if (ObjectUtil.isNotNull(texts) && texts.length != 0){
            StringBuilder builder = new StringBuilder();
            for (Object text : texts) {
                if (ObjectUtil.isNotNull(text)){
                    builder.append(text);
                }
            }
            return builder.toString();
        }else{
            return SysConstant.EMPTY_STR;
        }
    }

    /**
     * 字符串拼接
     * @return
     */
    public static String concatStr(List<String> strs, String concatStr){
        StringBuilder strB = new StringBuilder();
        if (CollectionUtil.isNotEmpty(strs)){
            if (SysConstant.EMPTY_STR.equals(concatStr)){
                for (String str : strs) {
                    strB.append(str);
                }
            }else{
                int max = strs.size() - 1;
                for (int i = 0; i <= max; i++) {
                    strB.append(removeStartEndStr(strs.get(i),concatStr));
                    if (i != max)
                        strB.append(concatStr);
                }
            }
            return strB.toString();
        }else{
            return SysConstant.EMPTY_STR;
        }
    }

    /**
     * 字符串移除凭借
     * @param path
     * @param concatStr
     * @return
     */
    public static String removeStartEndStr(String path,String concatStr) {
        if (ObjectUtil.isEmpty(path) || concatStr.equals(path))
            return SysConstant.EMPTY_STR;
        String tmp = path;
        if (tmp.startsWith(concatStr)){
            tmp = tmp.substring(1);
        }
        if (tmp.endsWith(concatStr)){
            tmp = tmp.substring(0,tmp.length() - 1);
        }
        return tmp;
    }
}
